/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package restauranteaplicacion.Modificaciones;

import restauranteaplicacion.Ingresos.*;
import Clases.Observador;
import CapaNegocios.CNProveedor;
import java.util.List;
import entidad.Proveedores;
import java.awt.Component;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;
import restauranteaplicacion.MDI;

/**
 *
 * @author Victor
 */
public class ModificacionProveedor extends javax.swing.JInternalFrame implements Observer{

    /**
     * Creates new form IngresoEmpleado
     */
    Observador obs;
    //DefaultTableModel modelo;
    @SuppressWarnings("empty-statement")
    public ModificacionProveedor() {
        initComponents();
        obs=new Observador();
        obs.addObserver(this);
    }
    


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        proveedorList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(controlrestaurante.pmf.getInstancia().getEMF().createEntityManager().createNamedQuery("Proveedores.findAll").getResultList());
        proveedores1 = new entidad.Proveedores();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel19 = new javax.swing.JLabel();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel20 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnAgregar = new javax.swing.JButton();
        btnAgregar2 = new javax.swing.JButton();
        jLabel21 = new javax.swing.JLabel();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable();
        btnCancelar = new javax.swing.JButton();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Tabla, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.activo}"), proveedores1, org.jdesktop.beansbinding.BeanProperty.create("activo"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Tabla, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.correo}"), proveedores1, org.jdesktop.beansbinding.BeanProperty.create("correo"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Tabla, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.descripcion}"), proveedores1, org.jdesktop.beansbinding.BeanProperty.create("descripcion"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Tabla, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.direccion}"), proveedores1, org.jdesktop.beansbinding.BeanProperty.create("direccion"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Tabla, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.idProveedores}"), proveedores1, org.jdesktop.beansbinding.BeanProperty.create("idProveedores"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Tabla, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.nitProveedor}"), proveedores1, org.jdesktop.beansbinding.BeanProperty.create("nitProveedor"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Tabla, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.nombre}"), proveedores1, org.jdesktop.beansbinding.BeanProperty.create("nombre"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Tabla, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.ordenentradaCollection}"), proveedores1, org.jdesktop.beansbinding.BeanProperty.create("ordenentradaCollection"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Tabla, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.telefono}"), proveedores1, org.jdesktop.beansbinding.BeanProperty.create("telefono"));
        bindingGroup.addBinding(binding);

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setIconifiable(true);
        setTitle("Creacion Meseros");
        setAutoscrolls(true);
        setMaximumSize(new java.awt.Dimension(984, 649));
        setMinimumSize(new java.awt.Dimension(984, 649));
        setPreferredSize(new java.awt.Dimension(984, 649));
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));

        jPanel1.setBackground(new java.awt.Color(54, 149, 240));
        jPanel1.setMaximumSize(new java.awt.Dimension(2000, 50));
        jPanel1.setMinimumSize(new java.awt.Dimension(246, 50));
        jPanel1.setPreferredSize(new java.awt.Dimension(1000, 50));

        jLabel4.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Proveedores");
        jPanel1.add(jLabel4);

        getContentPane().add(jPanel1);

        jPanel4.setPreferredSize(new java.awt.Dimension(988, 430));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setPreferredSize(new java.awt.Dimension(494, 500));

        jLabel19.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel19.setText("BUSCAR PROVEEDORES");

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel20.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel20.setText("<html>Buscar por nombre\n</html>");
        jLabel20.setBounds(10, 20, 100, 40);
        jLayeredPane1.add(jLabel20, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtNombre.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, proveedores1, org.jdesktop.beansbinding.ELProperty.create("${nombre}"), txtNombre, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNombreKeyTyped(evt);
            }
        });
        txtNombre.setBounds(120, 20, 390, 30);
        jLayeredPane1.add(txtNombre, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/pencil-3x.png"))); // NOI18N
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });
        btnModificar.setBounds(760, 20, 50, 40);
        jLayeredPane1.add(btnModificar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/restauranteaplicacion/Ingresos/x-3x.png"))); // NOI18N
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        btnEliminar.setBounds(840, 20, 50, 40);
        jLayeredPane1.add(btnEliminar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/plus-3x.png"))); // NOI18N
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        btnAgregar.setBounds(680, 20, 50, 40);
        jLayeredPane1.add(btnAgregar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnAgregar2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/reload-3x.png"))); // NOI18N
        btnAgregar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregar2ActionPerformed(evt);
            }
        });
        btnAgregar2.setBounds(520, 20, 50, 40);
        jLayeredPane1.add(btnAgregar2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel21.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel21.setText("PROVEEDORES");

        jLayeredPane3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, proveedorList, Tabla);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nombre}"));
        columnBinding.setColumnName("Nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nitProveedor}"));
        columnBinding.setColumnName("Nit");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${telefono}"));
        columnBinding.setColumnName("Telefono");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${correo}"));
        columnBinding.setColumnName("Correo");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${direccion}"));
        columnBinding.setColumnName("Direccion");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        Tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(Tabla);

        jScrollPane2.setBounds(10, 20, 840, 250);
        jLayeredPane3.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnCancelar.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/circle-x-3x.png"))); // NOI18N
        btnCancelar.setText("Salir");
        btnCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCancelar.setPreferredSize(new java.awt.Dimension(89, 53));
        btnCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        btnCancelar.setBounds(20, 300, 89, 53);
        jLayeredPane3.add(btnCancelar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel21)
                    .addComponent(jLabel19)
                    .addComponent(jLayeredPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 902, Short.MAX_VALUE)
                    .addComponent(jLayeredPane1))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel19)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel21)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLayeredPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 968, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 569, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel4);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

  
    
    private void CargarTabla(List<Proveedores> listatemp){
        proveedorList.clear();
        agregarLista(proveedorList,listatemp);
    }
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
        int fila =0;
        if(Tabla.getSelectedRow()>=0)
        {
            fila = JOptionPane.showInternalConfirmDialog(this, "Desea Eliminar "
                + " el producto", "Confirmacion",1, 0);
            if(fila == JOptionPane.OK_OPTION)
            {
                //eliminar el producto
                if(CNProveedor.eliminar(proveedores1))
                {
                    proveedorList.remove(proveedores1);
                    JOptionPane.showMessageDialog(this, "Se elimino Correctamente", 
                        "Informacion", JOptionPane.INFORMATION_MESSAGE);
                }
                else
                    JOptionPane.showMessageDialog(this, "No se ha podido Eliminar", 
                    "Informacion", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        else
            JOptionPane.showMessageDialog(this, "Debe de Seleccionar un elemento", 
                    "Informacion", JOptionPane.INFORMATION_MESSAGE);
       
    }//GEN-LAST:event_btnEliminarActionPerformed

  
    
    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        //TODO add your handling code here:  
       if(Tabla.getSelectedRow()>= 0)
       {
           IngresoProveedor ing = new IngresoProveedor(obs, proveedores1);
           MDI.getDesktopPane().add(ing);
           ing.show();
           obs.addObserver(ing);
           Desactivar();
           
       }
        
    }//GEN-LAST:event_btnModificarActionPerformed

    private void txtNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreKeyTyped
        // TODO add your handling code here:
        if(txtNombre.getText().length() >=2)
        {    
            CargarTabla(CNProveedor.buscarNombre(txtNombre.getText()));
        }
        if(txtNombre.getText().length() ==2)
            CargarTabla(CNProveedor.buscarTodos());
    }//GEN-LAST:event_txtNombreKeyTyped

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        // TODO add your handling code here:
        IngresoProveedor ing = new IngresoProveedor(obs);
        MDI.getDesktopPane().add(ing);
        ing.show();
        obs.addObserver(ing);
        Desactivar();
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void TablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaMouseClicked
        // TODO add your handling code here:
        //int fila = Tabla.rowAtPoint(evt.getPoint());
        //proveedores1 =proveedorList.get(fila);
    }//GEN-LAST:event_TablaMouseClicked

    private void btnAgregar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregar2ActionPerformed
        // TODO add your handling code here:
        txtNombre.setText("");
        buscarTodos();
    }//GEN-LAST:event_btnAgregar2ActionPerformed

    private void buscarTodos()
    {
        CargarTabla(CNProveedor.buscarTodos());
    }
    
    public void agregarLista(List<Proveedores>lista, List<Proveedores> nuevaLista)
    {
        Iterator<Proveedores> ite = nuevaLista.iterator();
        while(ite.hasNext())
        {
            Proveedores temp = ite.next();
            lista.add(temp);

        }
        
    }
    
    private void Desactivar()
    {
        for(Component x :this.jPanel2.getComponents())
            x.setEnabled(false);
        for(Component x : this.jLayeredPane1.getComponents())
            x.setEnabled(false);
        for(Component x : this.jLayeredPane3.getComponents())
            x.setEnabled(false);
    }
    private void Activar()
    {
        for(Component x :this.jPanel2.getComponents())
            x.setEnabled(true);
        for(Component x : this.jLayeredPane1.getComponents())
            x.setEnabled(true);
        for(Component x : this.jLayeredPane3.getComponents())
            x.setEnabled(true);
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tabla;
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnAgregar1;
    private javax.swing.JButton btnAgregar2;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private java.util.List<entidad.Proveedores> proveedorList;
    public static entidad.Proveedores proveedores1;
    private javax.swing.JTextField txtNombre;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    @Override
    public void update(Observable o, Object arg) {
       Activar();
       CargarTabla(CNProveedor.buscarNombre(txtNombre.getText()));
       
    }
}
